package experiment3.shudu001.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.TextView;

import experiment3.shudu001.R;

/**
 * Created by HP on 2025/4/26.
 * 该活动用于让用户选择数独游戏的难度级别
 */

public class DifficultyActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_difficulty);
        TextView title = (TextView)findViewById(R.id.title);
        Animation titleAnim = AnimationUtils.loadAnimation(this, R.anim.title_animation);
        title.startAnimation(titleAnim);

        // 初始化按钮
        Button easyButton = (Button) findViewById(R.id.easyButton);
        Button mediumButton = (Button) findViewById(R.id.mediumButton);
        Button hardButton = (Button) findViewById(R.id.hardButton);

        // 设置点击监听
        easyButton.setOnClickListener(this);
        mediumButton.setOnClickListener(this);
        hardButton.setOnClickListener(this);

    }

    private void setCompoundDrawables(Button button, int drawableRes) {
        Drawable drawable = getResources().getDrawable(drawableRes);
        drawable.setBounds(0, 0, 48, 48); // 设置图标大小
        button.setCompoundDrawables(drawable, null, null, null);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.easyButton:
                startGameWithAnimation(v, 1);
                break;
            case R.id.mediumButton:
                startGameWithAnimation(v, 2);
                break;
            case R.id.hardButton:
                startGameWithAnimation(v, 3);
                break;
        }
    }

    private void startGameWithAnimation(final View view, final int difficulty) {
        // 缩放动画
        ScaleAnimation scale = new ScaleAnimation(1f, 0.9f, 1f, 0.9f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        scale.setDuration(100);
        scale.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                ScaleAnimation reverse = new ScaleAnimation(0.9f, 1f, 0.9f, 1f,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
                reverse.setDuration(100);
                view.startAnimation(reverse);
                startGame(difficulty);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}
        });
        view.startAnimation(scale);
    }

    private void startGame(int difficulty) {
        Intent intent = new Intent(this, GameActivity.class);
        intent.putExtra("difficulty", difficulty);
        startActivity(intent);
    }
}